package xyz.ieden.hello.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.ieden.commons.entity.Book;
import xyz.ieden.hello.service.IBookService;

import java.util.List;
import java.util.Optional;

/**
 * Created by Gavin on 2017.08.05.
 */
@RestController
@RequestMapping(value = "book")
public class BookController {
    private static transient final Logger LOGGER = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private Optional<IBookService> bookService;

    @GetMapping(value = "", produces = "application/json;charset=UTF-8")
    public List<Book> getBook() {
        if (bookService.isPresent()) {
            List<Book> bookList = bookService.get().getBookList();
            return bookList;
        } else {
            return null;
        }
    }

    @GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
    public Book getBookById(@PathVariable(value = "id") Integer id) {
        if (bookService.isPresent()) {
            Book book = bookService.get().getBookById(id);
            LOGGER.info(book.toString());
            return book;
        } else {
            return null;
        }
    }
}
